package com.sunday.pinxinghui.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.makeramen.roundedimageview.RoundedImageView;
import com.sunday.common.utils.PixUtils;
import com.sunday.pinxinghui.R;
import com.sunday.pinxinghui.entity.IndexProduct;
import com.sunday.pinxinghui.model.products.ProductDetailActivity;
import com.sunday.pinxinghui.widgets.MyProgressBar;

import java.math.RoundingMode;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by 刘涛 on 2017/8/18.
 */

public class IndexMiaoShaAdapter extends RecyclerView.Adapter {


    private Context mContext;
    private List<IndexProduct> datas;

    public IndexMiaoShaAdapter(Context context, List<IndexProduct> datas) {
        this.mContext = context;
        this.datas = datas;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.list_index_miaosha_item, null);
        RecyclerView.LayoutParams layoutParams=new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        view.setLayoutParams(layoutParams);
        return new ListHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ListHolder productHolder = (ListHolder) holder;
        IndexProduct item1 = datas.get(position);
        if (!TextUtils.isEmpty(item1.getImage())) {
            Glide.with(mContext)
                    .load(item1.getImage())
                    .error(R.mipmap.ic_default)
                    .into(productHolder.productImg);
        }
        productHolder.productTitle.setText(item1.getProductName());
        productHolder.productDesc.setText(item1.getSummary());
        productHolder.priceNow.setText(String.format("¥%s", item1.getPrice().setScale(2, RoundingMode.HALF_UP)));
        productHolder.priceOld.setText(String.format("¥%s", item1.getCostPrice().setScale(2, RoundingMode.HALF_UP)));
        productHolder.priceOld.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);
        productHolder.saleNum.setText("已抢"+item1.getBuyCount()+"件");
        productHolder.layout.setTag(item1);
        productHolder.progressbar.setMax(item1.getLimitCount());
        productHolder.progressbar.setProgress(item1.getBuyCount());
    }

    @Override
    public int getItemCount() {
        return datas==null?0:datas.size();
    }

    class ListHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        @Bind(R.id.product_img)
        RoundedImageView productImg;
        @Bind(R.id.product_title)
        TextView productTitle;
        @Bind(R.id.progressbar)
        MyProgressBar progressbar;
        @Bind(R.id.price_old)
        TextView priceOld;
        @Bind(R.id.price_now)
        TextView priceNow;
        @Bind(R.id.btn_buy)
        TextView btnBuy;
        @Bind(R.id.layout)
        RelativeLayout layout;
        @Bind(R.id.sale_num)
        TextView saleNum;
        @Bind(R.id.product_desc)
        TextView productDesc;


        public ListHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            layout.setOnClickListener(this);
            RelativeLayout.LayoutParams params= (RelativeLayout.LayoutParams) productImg.getLayoutParams();
            params.width= PixUtils.dip2px(mContext,150);
            params.height=PixUtils.dip2px(mContext,100);
            productImg.setLayoutParams(params);
        }

        @Override
        public void onClick(View v) {
            IndexProduct item = (IndexProduct) v.getTag();
            Intent intent = new Intent(mContext, ProductDetailActivity.class);
            //intent.putExtra("type", );
             intent.putExtra("productId", item.getProductId());
            //intent.putExtra("limitId", timeLimitId);
            mContext.startActivity(intent);
        }
    }
}
